package com.yootk.action;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/thymeleaf/*")
public class ThymeleafAction {

    @RequestMapping("view")
    public ModelAndView view(String message) {
        ModelAndView mav = new ModelAndView("message/message_show");
        mav.addObject("message", message);
        mav.addObject("title", "沐言科技");
        mav.addObject("content", "www.yootk.com");
        return mav;
    }

    @RequestMapping("view2")
    public String view2(String message, Model model) {
        model.addAttribute("message", message);
        model.addAttribute("title", "沐言科技");
        model.addAttribute("content", "www.yootk.com");
        return "message/message_show";
    }

    @RequestMapping("path")
    public String path() {
        return "message/message_path";  // 跳转路径
    }

    @RequestMapping("i18n")
    public String i18n() {
        return "message/message_i18n";  // 跳转路径
    }

    @RequestMapping("attribute")
    public String attribute(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("message", "【REQUEST】沐言科技：www.yootk.com");
        request.getSession().setAttribute("message", "【SESSION】沐言科技：www.yootk.com");
        request.getServletContext().setAttribute("message", "【APPLICATION】沐言科技：www.yootk.com");
        return "message/message_attribute";
    }

    @RequestMapping("include")
    public String include() {
        return "message/message_include";
    }

    @RequestMapping("handle")
    public String handle(Model model) throws Exception {
        model.addAttribute("message", "www.YOOTK.com");
        model.addAttribute("language", Set.of("Java", "Python", "GoLang"));
        model.addAttribute("infos", Map.of("yootk", "yootk.com", "edu", "edu.yootk.com"));
        return "message/message_handle";
    }
}
